/**
 * @file s_weight.h
 * @brief 
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2023-12-05
 * 
 * @copyright Copyright (c) 2023  by xuesong
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-12-05     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#ifndef __S_WEIGHT_H_
#define __S_WEIGHT_H_

#include "sdk_config.h"
#include "log.h"
#include "key.h"
#include "OSAL.h"
#include "gpio.h"
#include "s_sample.h"
#include "Display.h"
//----------------------------------------------------
#define SCALE_RANGE_CONT    3           //兼容称量程选择
#define CALIBRATE_PIONT     4           //多点标定
#define EXSIT_FLAG          0x55AA
#define CAL_ADCSUB_MUL_MIN (1)	
#define CAL_ADCSUB_MUL_MAX (256)
//----------------------------------------------------
typedef enum
{
    WEIGHT_UNIT_G  = 0x00,
    WEIGHT_UNIT_OZ,
    WEIGHT_UNIT_MAX,
}WEIGHT_UNIT;
//----------------------------------------------------
typedef struct{
	union {
        struct
        {
            unsigned char init 		        : 1;    // 
            unsigned char CalOK             : 1;    //
            unsigned char ZeroOK            : 1;    //
            unsigned char WtEnable          : 1;    //
            unsigned char WtNew             : 1;    //
            unsigned char Load              : 1;    //
            unsigned char Lock              : 1;    //
            unsigned char overLoad          : 1;    //
            unsigned char Atuotare          : 1;    // 启用自动去皮
            unsigned char tare              : 1;    // 待去皮
            unsigned char tared             : 1;    // 已去皮
            unsigned char Calibr            : 1;    // 标定中
            unsigned char CalibrAtoZrEn     : 1;    // 标定 自动找零 使能
            unsigned char TareNeedBZ	    : 1;    // 去皮需要想一下蜂鸣器
        }Bit;
        uint16_t Byte;
    }Flag;
    uint16_t DivisionAdc;
    uint32_t ZeroPointADC;
    uint32_t TarePointADC;
    int32_t NetWeight;
    int32_t GrossWeight;
}Weight_t;

//----------------------------------------------------------
#pragma pack (1)
typedef struct{
    uint16_t Existflag;
    uint16_t DivisionWeight;
    uint32_t Calibration[CALIBRATE_PIONT];
    uint32_t MaxWeight;
    uint16_t MinWeight;
    uint8_t  WeightRangIndex;             //称重范围
    uint16_t Crccheck_sum;
}Scale_data_t;
#pragma pack ()
//----------------------------------------------------------
extern const uint16_t ScaleMinWeight[SCALE_RANGE_CONT];
extern const uint32_t ScaleMaxWeight[SCALE_RANGE_CONT];
extern const uint32_t ScaleRang[SCALE_RANGE_CONT];
extern const uint32_t STAND_WEIGHT_POINT[CALIBRATE_PIONT];
extern Weight_t ScaleWeight;
extern Scale_data_t ScaleInfor;
//----------------------------------------------------------
void InitScale(void);
void CalculateWeightProcess(void);
uint64_t RoundDiv(uint64_t dividend,uint32_t divisor);
void ClrWeightDisplayBuffer(Display_t *p_display);
void displayWeight(int32_t weight,uint8_t weightUnit);
void F_LoadTraceZero(void);
//----------------------------------------------------------
#endif

